﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;


public partial class Web_Question : System.Web.UI.Page
{
    protected bool SavedAndRedirect = false;
    protected List<Train> ts;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FetchCountry();
            FetchKind();
        }
        ///
        // 初始化
        ///
        CmsDataContext db = new CmsDataContext();
        var train = (from aT in db.Train where (aT.Started - DateTime.Now).Days >= 0 orderby aT.Registed descending select aT).Take(6);
        ts = train.ToList<Train>();
        var px2 = (from aA in db.Article where aA.Verified != null orderby aA.Verified descending select aA).Take(6);
        Repeater3.DataSource = px2;
        Repeater3.DataBind();
    }

    private void FetchCountry()
    {
        Country.Items.Clear();

        XmlDataSource xds = new XmlDataSource();
        xds.DataFile = Toolkit.GetRealDataFilePath("Street.xml");
        xds.XPath = "/Root/Country";

        Country.DataSource = xds;
        Country.DataTextField = "name";
        Country.DataValueField = "name";
        Country.DataBind();

        Country.Items.Insert(0, new ListItem("请选择", ""));

        Street.Items.Clear();
        Street.Items.Insert(0, new ListItem("请选择", ""));

        Village.Items.Clear();
        Village.Items.Insert(0, new ListItem("请选择", ""));
    }

    private void FetchStreet()
    {        
        Street.Items.Clear();

        if (Country.SelectedValue != ""){
            XmlDataSource xds = new XmlDataSource();
            xds.DataFile = Toolkit.GetRealDataFilePath("Street.xml");
            xds.XPath =string.Format( "/Root/Country[@name='{0}']/Street", Country.SelectedValue);

            Street.DataSource = xds;
            Street.DataTextField = "name";
            Street.DataValueField = "name";
            Street.DataBind();
        }

        Street.Items.Insert(0, new ListItem("请选择", ""));

        Village.Items.Clear();
        Village.Items.Insert(0, new ListItem("请选择", ""));
    }

    private void FetchVillage()
    {
        Village.Items.Clear();

        if (Street.SelectedValue != "")
        {
            XmlDataSource  xds = new XmlDataSource();
            xds.DataFile = Toolkit.GetRealDataFilePath("Street.xml");
            xds.XPath = string.Format("/Root/Country[@name='{0}']/Street[@name='{1}']/Village", Country.SelectedValue, Street.SelectedValue);

            Village.DataSource = xds;
            Village.DataTextField = "name";
            Village.DataValueField = "name";
            Village.DataBind();
        }

        Village.Items.Insert(0, new ListItem("请选择", ""));
    }
    protected string GetTopicID(string tName)
    {
        CmsDataContext db = new CmsDataContext();
        var t = from aT in db.Topic where aT.Name.Equals(tName) select aT;
        if (t.Count() != 0)
        {
            return t.First().ID;
        }
        else
        {
            return "";
        }
    }
    protected void Country_SelectedIndexChanged(object sender, EventArgs e)
    {
        FetchStreet();

        FetchTeachers();
    }
    protected void Street_SelectedIndexChanged(object sender, EventArgs e)
    {
        FetchVillage();
    }

    private void FetchKind()
    {
        Kind.Items.Clear();

        XmlDataSource xds = new XmlDataSource();
        xds.DataFile = Toolkit.GetRealDataFilePath("Course.xml");
        xds.XPath = "/Root/Kind";

        Kind.DataSource = xds;
        Kind.DataTextField = "name";
        Kind.DataValueField = "name";
        Kind.DataBind();

        Kind.Items.Insert(0, new ListItem("请选择", ""));


        Subject.Items.Clear();
        Subject.Items.Insert(0, new ListItem("请选择", ""));

        Course.Items.Clear();
        Course.Items.Insert(0, new ListItem("请选择", ""));
    }

    private void FetchSubject()
    {
        Subject.Items.Clear();

        if (Kind.SelectedValue != "")
        {
            XmlDataSource xds = new XmlDataSource();
            xds.DataFile = Toolkit.GetRealDataFilePath("Course.xml");
            xds.XPath = string.Format("/Root/Kind[@name='{0}']/Subject", Kind.SelectedValue);

            Subject.DataSource = xds;
            Subject.DataTextField = "name";
            Subject.DataValueField = "name";
            Subject.DataBind();
        }

        Subject.Items.Insert(0, new ListItem("请选择", ""));

        Course.Items.Clear();
        Course.Items.Insert(0, new ListItem("请选择", ""));
    }

    private void FetchCourse()
    {
        Course.Items.Clear();

        if (Subject.SelectedValue != "")
        {
            XmlDataSource xds = new XmlDataSource();
            xds.DataFile = Toolkit.GetRealDataFilePath("Course.xml");
            xds.XPath = string.Format("/Root/Kind[@name='{0}']/Subject[@name='{1}']/Course", Kind.SelectedValue, Subject.SelectedValue);

            Course.DataSource = xds;
            Course.DataTextField = "name";
            Course.DataValueField = "name";
            Course.DataBind();
        }

        Course.Items.Insert(0, new ListItem("请选择", ""));
    }

    private void FetchTeachers()
    {
        Teachers.Items.Clear();

        CmsDataContext db = new CmsDataContext();
        var q = from aT in db.Teacher
                where (aT.Verfied != null) && (aT.Courses.Contains("其它")) && (aT.District == Country.SelectedValue)
                orderby aT.Rank, aT.Name
                select new { ID = aT.ID, Name = aT.Name };

        Teachers.DataSource = q.ToList();
        Teachers.DataTextField = "Name";
        Teachers.DataValueField = "ID";
        Teachers.DataBind();
    }

    protected void Kind_SelectedIndexChanged(object sender, EventArgs e)
    {
        FetchSubject();
    }
    protected void Subject_SelectedIndexChanged(object sender, EventArgs e)
    {
        FetchCourse();
    }
    
    protected void Save_Click(object sender, EventArgs e)
    {
        CmsDataContext db = new CmsDataContext();

        try
        {
            Question question = new Question();
            question.ID = Toolkit.NextUUID;

            db.Question.InsertOnSubmit(question);

            question.Name = Name.Text.Trim();
            question.Phone = Telphone.Text.Trim();
            question.Country = Country.SelectedValue.Trim();
            question.Street = Street.SelectedValue.Trim();
            question.Village = Village.SelectedValue.Trim();
            question.Address = Address.Text.Trim();
            question.Title = QuestionTitle.Text.Trim();
            question.Memo = Memo.Text.Trim();
            question.Kind = Kind.Text.Trim();
            question.Subject = Subject.Text.Trim();
            question.Course = Course.Text.Trim();
            question.Time = DateTime.Now;

            StringBuilder sb = new StringBuilder();
            foreach (ListItem li in Teachers.Items)
            {
                if (li.Selected)
                {
                    if (sb.Length != 0)
                    {
                        sb.Append(",");
                    }

                    sb.Append(li.Value);
                }
            }

            question.Teachers = sb.ToString();
            question.Description = "";
            question.Verified = DateTime.Now;

            db.SubmitChanges();

            Response.Write("<script>alert('您的问题已经提交，请等待老师回答。');document.location='./ListQuestions.aspx';</script>");
        }
        catch (Exception ee)
        {
            Response.Write("<script>alert(\"问题提交过程中发生错误。原因：" + ee.Message + "。\");</script>");
        }
    }
}